1881E - Block Sequence - CodeForces Solution


dp

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define vc vector
#define lson l,mid,rt<<1
#define rson m+1,r,rt<<1|1
#define sall(a) sort(a.begin(),a.end())
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define range(x) begin(x),end(x)
#define stop system("pause");
//#define int long long
using namespace std;
typedef long long ll;
typedef pair<int,int>pii;
const int N=2e5+10;
const ll inf=1e9;
const int mod=1e9+7;
int T,n,m,d,k;
void solve()
{
    cin>>n;
    vc<int>a(n+1),f(n+3);
    for(int i=1;i<=n;i++)   cin>>a[i];
    f[n]=1;
    for(int i=n-1;i>=1;i--){
        if(a[i]<=n-i){
            f[i]=min(f[i+1]+1,f[i+a[i]+1]);
        }else {
            f[i]=f[i+1]+1;
        }
    }
    cout<<f[1]<<endl;
}
signed main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    
    cin>>T;
    while(T--)
    {
        solve();
    }
    stop
    return 0;
}


Comments

Submit
0 Comments
More Questions

450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers